package com.tanqidi.arcgisandroid.ui.envConfig

import android.os.Bundle
import android.text.TextUtils
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import androidx.core.widget.addTextChangedListener
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import com.tanqidi.arcgisandroid.R
import com.tanqidi.arcgisandroid.base.utils.SPUtils
import com.tanqidi.arcgisandroid.base.utils.getSPUtils
import com.tanqidi.arcgisandroid.databinding.FragmentEnvConfigBinding
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class EnvConfigFragment : Fragment() {

    private val viewModel: EnvConfigViewModel by viewModels()
    private lateinit var binding:FragmentEnvConfigBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = FragmentEnvConfigBinding.inflate(layoutInflater)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        initTopBar()
        initView()
        return binding.root
    }

    private fun initTopBar() {
        binding.topAppBar.setNavigationOnClickListener {
            findNavController().navigateUp()
        }
    }

    private fun initView() {
        val basemapTypeList = listOf(getString(R.string.mapbox), getString(R.string.arcgis), getString(R.string.tiandi))
        val instance = getSPUtils()
        binding.baseMap.apply {
            setText(instance.getString(getString(R.string.basemap), null), false)
            val arrayAdapter = ArrayAdapter(requireContext(), R.layout.cat_exposed_dropdown_popup_item, basemapTypeList)
            setAdapter(arrayAdapter)
            setOnItemClickListener { parent, view, position, id ->
                instance.put(getString(R.string.basemap), basemapTypeList[position])
            }
        }

        binding.spaceBetween.apply {
            //回显
            setText(instance.getInt(getString(R.string.spaceBetween), 10).toString())
            //内容变化
            addTextChangedListener {
                val content = it.toString().trim()
                if (!TextUtils.isEmpty(content)) {
                    instance.put(getString(R.string.spaceBetween), content.toInt())
                } else {
                    //默认10米
                    instance.put(getString(R.string.spaceBetween), 10)
                }
            }
        }

        val statusTypeList = listOf(getString(R.string.open), getString(R.string.close),)

        //是否显示当前轨迹记录
        binding.isShowCurrentLocationHistory.apply {
            setText(instance.getString(getString(R.string.isShowCurrentLocationHistory), getString(R.string.close)), false)
            val arrayAdapter = ArrayAdapter(requireContext(), R.layout.cat_exposed_dropdown_popup_item, statusTypeList)
            setAdapter(arrayAdapter)
            setOnItemClickListener { parent, view, position, id ->
                val item = statusTypeList[position]
                when (item) {
                    getString(R.string.open) -> {
                        instance.put(getString(R.string.isShowCurrentLocationHistory), item)
                    }
                    getString(R.string.close) -> {
                        instance.remove(getString(R.string.isShowCurrentLocationHistory))
                    }
                }

            }
        }


        //是否开启轨迹记录
        binding.isOpenLocationHistory.apply {
            setText(instance.getString(getString(R.string.isOpenLocationHistory), getString(R.string.close)), false)
            val arrayAdapter = ArrayAdapter(requireContext(), R.layout.cat_exposed_dropdown_popup_item, statusTypeList)
            setAdapter(arrayAdapter)
            setOnItemClickListener { parent, view, position, id ->
                val item = statusTypeList[position]
                when (item) {
                    getString(R.string.open) -> {
                        instance.put(getString(R.string.isOpenLocationHistory), item)
                    }
                    getString(R.string.close) -> {
                        instance.remove(getString(R.string.isOpenLocationHistory))
                    }
                }

            }
        }

        /**
         * 中央经线 zyjx
         */
        binding.zyjx.apply {
            setText(instance.getString(getString(R.string.zyjx), "75"), false)
            val list = listOf("75","81","87","93","99","105","111","117","123","129","135")
            val arrayAdapter = ArrayAdapter(requireContext(), R.layout.cat_exposed_dropdown_popup_item, list)
            setAdapter(arrayAdapter)
            setOnItemClickListener { parent, view, position, id ->
                val item = list[position]
                instance.put(getString(R.string.zyjx), item)
            }
        }
    }

}